package com.gzhryc.shared_device.cdb.core.protocol.v1.msg;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.secruity.CRC16;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * 设备指定借出命令响应
 * {BW,[相同消息 ID],[设备 ID],[订单 ID],[结果码],[槽位 ID],[移动电源 ID],[移动电源电量],[移动电源锁状态],[移动电源线状态],[校验码]}
 * @author zhanghao
 *
 */
public class FBResponse implements Serializable{
	
	private static final long serialVersionUID = 1L;

	public final static String CMD = "FB";

	private String cmd = CMD;
	private String messageId;
	private String deviceSn;
	private String tradeNo;
	private Integer resultCode;	//结果码
	private Integer slotNum;		//槽位 ID
	private String mobilePowerId = "";    		  //移动电源 ID
	private Double mobilePowerSupplyVolume;  	 //移动电源电量
	private String mobilePowerLockStatus = "";    //移动电源锁状态
	private String mobilePowerWireStatus = "";	  //移动电源线状态
	
	public FBResponse() {}
	
	public FBResponse(BaseMessage baseMessage) {
		String[] data = baseMessage.getData();
		this.deviceSn = baseMessage.getDeviceSn();
		this.messageId = baseMessage.getMessageId();

		if(data != null) {
			if(data.length >= 1) {
				this.tradeNo = data[0];
			}
			if(data.length >= 2) {
				this.resultCode = Integer.valueOf(data[1]);
			}
			if(data.length >= 3) {
				this.slotNum = Integer.valueOf(data[2]);
			}
			if(data.length >= 4) {
				this.mobilePowerId = data[3];
			}
			if(data.length >= 5) {
				String temp = data[4];
				if(StringUtils.isNotBlank(temp) && StringTools.isNumber(temp)) {
					this.mobilePowerSupplyVolume = Double.valueOf(temp);
				}
			}
			if(data.length >= 6) {
				this.mobilePowerLockStatus = data[5];
			}
			if(data.length >= 7) {
				this.mobilePowerWireStatus = data[6];
			}
		}
	}
	
	public String getCmd() {
		return cmd;
	}
	public void setCmd(String cmd) {
		this.cmd = cmd;
	}
	
	public String getTradeNo() {
		return tradeNo;
	}

	public void setTradeNo(String tradeNo) {
		this.tradeNo = tradeNo;
	}

	public String getMessageId() {
		return messageId;
	}

	public String getDeviceSn() {
		return deviceSn;
	}

	public void setDeviceSn(String deviceSn) {
		this.deviceSn = deviceSn;
	}

	public Integer getResultCode() {
		return resultCode;
	}

	public void setResultCode(Integer resultCode) {
		this.resultCode = resultCode;
	}

	public Integer getSlotNum() {
		return slotNum;
	}

	public void setSlotNum(Integer slotNum) {
		this.slotNum = slotNum;
	}

	public String getMobilePowerId() {
		return mobilePowerId;
	}

	public void setMobilePowerId(String mobilePowerId) {
		this.mobilePowerId = mobilePowerId;
	}

	public Double getMobilePowerSupplyVolume() {
		return mobilePowerSupplyVolume;
	}

	public void setMobilePowerSupplyVolume(Double mobilePowerSupplyVolume) {
		this.mobilePowerSupplyVolume = mobilePowerSupplyVolume;
	}

	public String getMobilePowerLockStatus() {
		return mobilePowerLockStatus;
	}

	public void setMobilePowerLockStatus(String mobilePowerLockStatus) {
		this.mobilePowerLockStatus = mobilePowerLockStatus;
	}

	public String getMobilePowerWireStatus() {
		return mobilePowerWireStatus;
	}

	public void setMobilePowerWireStatus(String mobilePowerWireStatus) {
		this.mobilePowerWireStatus = mobilePowerWireStatus;
	}

	public void setMessageId(String messageId) {
		this.messageId = messageId;
	}
	
	public String getData() {
		String data = cmd+","+messageId+","+deviceSn+","+tradeNo+","+resultCode+","+slotNum+","+mobilePowerId+","+mobilePowerSupplyVolume+","+mobilePowerLockStatus+","+mobilePowerWireStatus;
		return data;
	}
	
	public String getMessage() {
		String data = getData();
		String crc16 = CRC16.get((data+","+ BaseMessage.MESSAGE_KEY).getBytes());
		return "{"+data+","+crc16+"}";
	}
}
